# -*- coding: utf-8 -*-

#    Copyright (C) 2014 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>

#    This file is part of OdCollect.

#    OdCollect is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    OdCollect is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with OdCollect.  If not, see <http://www.gnu.org/licenses/>.


from web_lib.webutils import Response, expose , json , salvaInSessione, leggiInSessione , permalinkaTitle, pagination
from lib.utils import *
from web_lib.page import Page

@expose('/pg/faq_detail/<permalink>')
@expose('/faq_detail/<permalink>')
def faq_detail(req,subdomain=None, permalink=None):
    from dao.Faq import Faq
    faq = Faq(req=req).select(permalink=permalink)
    if faq:

        mo = faq[0]
        mo.clicks = int(mo.clicks or 0)+1
        mo.persist()

        pageData = {'file' : "/main/faq_detail",
                    "emb" : False,
                    "faq":mo}
        return Page(req).render(pageData)
    else:
        pageData = {'file' : 'not_found'}
        return Page(req).render(pageData)

@expose('/faq')
def faq_list(req, static=None, subdomain=""):
    #count = News().count(searchkey=req.form.get('searchkey'))
    batch = 10
    #args = pagination(req,batch,count)
    #args["page_list"] = "/news_list_home"
    from dao.Faq import Faq
    faqs = Faq().select(batchSize=batch,active=True)
    pageData = {'file' : 'main/faq_list',
                #"subdomain": subdomain,
                #"args":args,
                "faqs" :faqs
                }
    return Page(req).render(pageData)

@expose('/faq_answer')
def faq_answer(req,subdomain=None):
    from dao.Faq import Faq
    emb=0
    idr = req.form.get("idr")
    embedded = req.form.get("embedded")
    if embedded:
        emb= 1
    dao = Faq(req=req).getRecord(id=idr)
    if dao:
        dao.clicks = int(dao.clicks or 0)+1
        dao.persist()
        pageData = {'file' : 'main/faq_detail',
                    "subdomain": subdomain,
                    "emb":emb,
                    "faq":dao}
        return Page(req).render(pageData)
